<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>

<?php
include('order_model.php');
session_start();
if(isset($_SESSION['order']) && isset($_SESSION['cart']) &&(! isset($_SESSION['user'])or $_SESSION['user']=="null"))
{
  $order=$_SESSION['order'];
 $name=$order[0];
   $email=$order[1];
   $pass=$order[2];
   $addr=$order[3];
   $phone=$order[4];
   $toName=$order[5];
   $toAdd=$order[6];
   $des=$order[7];
   $paymentId=$order[8];
   $bankid=$order[9];
  $nowdate=getdate();
	 $createdate=$nowdate['year'].'-'.$nowdate['mon'].'-'.$nowdate['mday'];
	 $total=$_SESSION['total'];
	
	//THem khach hang vao table customer
  insertCustomer  ($name, $addr, $email, $phone);
  //lay ma khach hang vau insert
  $cus=getCustomerId($email);
  $cusId=$cus[0];
  //them vao bang user khach hang vua thanh toan
  insertUser ( $email, $pass, $cusId);
  // them cao bang hoa don cho khach hang vua mau hang
  insertOrder($createdate, $toAdd, $toName, $des, $paymentId,$total, $cusId);
  //lay ma hoa don vau insert
  $or=getOrderId($cusId);
  $orid=$or[0];
   //them vao bang chi tiet hoa don danhh sach san phamr maf khach hang vua mua:
  foreach($_SESSION['cart'] as $key=>$value)
								{
								 $item[]=$key;
								}
	$str=implode(",",$item);
	$sql="select * from product where ProductID in ($str) order by ProductID desc";
	$rl= mysql_query($sql, getConnect());
						
	while ($pr = mysql_fetch_array($rl))
			{
					$arr[]=$pr;
			}
	foreach ($arr as $items)
								{
								$dis=$items[9];
								$tk=$items[4]*$dis/100;
								 $pri=$items[4]-$tk;
								 $qty=$_SESSION['cart'][$items[0]];
								 $amo=$pri*$qty;	
								 inserOrderDetail($items[0], $orid, $qty, $amo);
								 }		
		unset($_SESSION['cart']);
		unset($_SESSION['order']);  
		header ('Location:/cfshop/ShoppingCart/success.php');
}
elseif (isset($_SESSION['order']) && isset($_SESSION['cart']) &&isset($_SESSION['user'])&& $_SESSION['user'][5]==0)
{
   $order=$_SESSION['order'];
 $name=$order[0];
   $email=$order[1];
   $pass=$order[2];
   $addr=$order[3];
   $phone=$order[4];
   $toName=$order[5];
   $toAdd=$order[6];
   $des=$order[7];
   $paymentId=$order[8];
   $bankid=$order[9];
  $nowdate=getdate();
	 $createdate=$nowdate['year'].'-'.$nowdate['mon'].'-'.$nowdate['mday'];
	 $total=$_SESSION['total'];
	 $userId=$_SESSION['user'][0];
	 //THem khach hang vao table customer
  insertCustomer  ($name, $addr, $email, $phone);
  //lay ma khach hang vau insert
  $cus=getCustomerId($email);
  $cusId=$cus[0];
  //update customerid cho user
  updateUser($cusId,$userId);
	 // them cao bang hoa don cho khach hang vua mau hang
  insertOrder($createdate, $toAdd, $toName, $des, $paymentId,$total, $cusId);
  //lay ma hoa don vau insert
  $or=getOrderId($cusId);
  $orid=$or[0];
   //them vao bang chi tiet hoa don danhh sach san phamr maf khach hang vua mua:
  foreach($_SESSION['cart'] as $key=>$value)
								{
								 $item[]=$key;
								}
	$str=implode(",",$item);
	$sql="select * from product where ProductID in ($str) order by ProductID desc";
	$rl= mysql_query($sql, getConnect());
						
	while ($pr = mysql_fetch_array($rl))
			{
					$arr[]=$pr;
			}
	foreach ($arr as $items)
								{
								$dis=$items[9];
								$tk=$items[4]*$dis/100;
								 $pri=$items[4]-$tk;
								 $qty=$_SESSION['cart'][$items[0]];
								 $amo=$pri*$qty;	
								 inserOrderDetail($items[0], $orid, $qty, $amo);
								 }		
		unset($_SESSION['cart']);
		unset($_SESSION['order']);  
		$_SESSION['user'][5]=$cusId;
		header ('Location:/cfshop/ShoppingCart/success.php');
	 
}
elseif (isset($_SESSION['order']) && isset($_SESSION['cart']) &&isset($_SESSION['user'])&& $_SESSION['user'][5]>0) {
	 $order=$_SESSION['order'];
 $name=$order[0];
   $email=$order[1];
   $pass=$order[2];
   $addr=$order[3];
   $phone=$order[4];
   $toName=$order[5];
   $toAdd=$order[6];
   $des=$order[7];
   $paymentId=$order[8];
   $bankid=$order[9];
  $nowdate=getdate();
	 $createdate=$nowdate['year'].'-'.$nowdate['mon'].'-'.$nowdate['mday'];
	 $total=$_SESSION['total'];
	 $userId=$_SESSION['user'][0];
	 $cusId=$_SESSION['user'][5];
	 updateCustomer($cusId, $name, $addr, $email, $phone);
	 insertOrder($createdate, $toAdd, $toName, $des, $paymentId,$total, $cusId);
  //lay ma hoa don vau insert
  $or=getOrderId($cusId);
  $orid=$or[0];
   //them vao bang chi tiet hoa don danhh sach san phamr maf khach hang vua mua:
  foreach($_SESSION['cart'] as $key=>$value)
								{
								 $item[]=$key;
								}
	$str=implode(",",$item);
	$sql="select * from product where ProductID in ($str) order by ProductID desc";
	$rl= mysql_query($sql, getConnect());
						
	while ($pr = mysql_fetch_array($rl))
			{
					$arr[]=$pr;
			}
	foreach ($arr as $items)
								{
								$dis=$items[9];
								$tk=$items[4]*$dis/100;
								 $pri=$items[4]-$tk;
								 $qty=$_SESSION['cart'][$items[0]];
								 $amo=$pri*$qty;	
								 inserOrderDetail($items[0], $orid, $qty, $amo);
								 }		
		unset($_SESSION['cart']);
		unset($_SESSION['order']);  
		header ('Location:/cfshop/ShoppingCart/success.php');
}

?>